package cn.zmall.search.service.impl;

import cn.zmall.common.exception.ZmallException;
import cn.zmall.common.utils.HttpUtil;
import cn.zmall.manager.dto.EsCount;
import cn.zmall.manager.dto.EsInfo;
import cn.zmall.manager.dto.front.SearchItem;
import cn.zmall.search.mapper.ItemMapper;
import cn.zmall.search.mapper.SearchMapper;
import cn.zmall.search.service.SearchItemService;
import com.alibaba.dubbo.common.utils.StringUtils;
import com.google.gson.Gson;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.net.InetAddress;
import java.util.List;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;

/**
 * @Author BinBini
 * @Date 2020/11/5 16:25
 * @Version 1.0
 */
@Service
public class SearchItemServiceImpl implements SearchItemService {

    private final static Logger log= LoggerFactory.getLogger(SearchItemServiceImpl.class);

    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private SearchMapper searchMapper;

    @Value("localhost")
    private String ES_CONNECT_IP;

    @Value("9200")
    private String ES_NODE_CLIENT_PORT;

    @Value("elasticsearch")
    private String ES_CLUSTER_NAME;

    @Value("item")
    private String ITEM_INDEX;

    @Value("itemList")
    private String ITEM_TYPE;

    @Override
    public int importAllItems() {
        try {
            Settings settings = Settings.builder()
                    .put("cluster.name", ES_CLUSTER_NAME).build();
            TransportClient client = new PreBuiltTransportClient(settings)
                    .addTransportAddress(new TransportAddress(InetAddress.getByName(ES_CONNECT_IP), 9300));

            //批量添加
            BulkRequestBuilder bulkRequest = client.prepareBulk();

            //查询商品列表
            List<SearchItem> itemList = itemMapper.getItemList();

            //遍历商品列表
            for (SearchItem searchItem : itemList) {
                String image=searchItem.getProductImageBig();
                if (image != null && !"".equals(image)) {
                    String[] strings = image.split(",");
                    image=strings[0];
                }else{
                    image="";
                }
                searchItem.setProductImageBig(image);
                bulkRequest.add(client.prepareIndex(ITEM_INDEX, ITEM_TYPE, String.valueOf(searchItem.getProductId()))
                        .setSource(jsonBuilder()
                                .startObject()
                                .field("productId", searchItem.getProductId())
                                .field("salePrice", searchItem.getSalePrice())
                                .field("productName", searchItem.getProductName())
                                .field("subTitle", searchItem.getSubTitle())
                                .field("productImageBig", searchItem.getProductImageBig())
                                .field("categoryName", searchItem.getCategoryName())
                                .field("cid", searchItem.getCid())
                                .endObject()
                        )
                );
            }

            BulkResponse bulkResponse = bulkRequest.get();

            log.info("更新索引成功");

            client.close();
        }catch (Exception e) {
            throw new ZmallException("导入ES索引库出错，请再次尝试");
        }
//        try {
//            List<SearchItem> itemList = itemMapper.getItemList();
//
//            for(SearchItem searchItem : itemList) {
//                String image=searchItem.getProductImageBig();
//                if (image != null && !"".equals(image)) {
//                    String[] strings = image.split(",");
//                    image=strings[0];
//                }else{
//                    image="";
//                }
//                searchItem.setProductImageBig(image);
//                searchMapper.save(searchItem);
//                log.info("更新索引成功");
//            }
//        }catch (Exception e) {
//            throw new ZmallException("导入ES索引库出错，请再次尝试");
//        }

        return 1;
    }

    @Override
    public EsInfo getEsInfo() {
        String healthUrl="http://"+ES_CONNECT_IP+":"+ES_NODE_CLIENT_PORT+"/_cluster/health";
        String countUrl="http://"+ES_CONNECT_IP+":"+ES_NODE_CLIENT_PORT+"/_count";
        String healthResult= HttpUtil.sendGet(healthUrl);
        String countResult=HttpUtil.sendGet(countUrl);
        if(StringUtils.isBlank(healthResult)||StringUtils.isBlank(countResult)){
            throw new ZmallException("连接集群失败，请检查ES运行状态");
        }
        EsInfo esInfo=new EsInfo();
        EsCount esCount=new EsCount();
        try {
            esInfo=new Gson().fromJson(healthResult,EsInfo.class);
            esCount=new Gson().fromJson(countResult,EsCount.class);
            esInfo.setCount(esCount.getCount());
        }catch (Exception e){
            e.printStackTrace();
            throw new ZmallException("获取ES信息出错");
        }

        return esInfo;
    }
}
